package com.billdowney.service.impl.user;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.billdowney.dao.user.UserLoginRecordDao;
import com.billdowney.entity.user.UserLoginInfo;
import com.billdowney.entity.user.UserLoginRecord;
import com.billdowney.exception.BizException;
import com.billdowney.service.impl.base.BaseServiceImpl;
import com.billdowney.service.interfaces.system.log.SystemLogService;
import com.billdowney.service.interfaces.user.UserLoginRecordService;
import com.billdowney.util.CommonUtil;
import com.billdowney.util.log.SystemLogHandleType;
import com.billdowney.util.request.RequestUtil;
import com.billdowney.entity.user.UserLoginRecordStatus;

/**
 * @ClassName: UserLoginRecordServiceImpl
 * @Description: 用户登录记录接口实现
 * @author 超级小富翁 - BillDowney
 * @date 2018年4月13日 下午4:25:23
 */
@Service
public class UserLoginRecordServiceImpl extends BaseServiceImpl<UserLoginRecordDao, UserLoginRecord, String>
		implements UserLoginRecordService {

	@Autowired
	private SystemLogService systemLogService;

	@Resource(type = UserLoginRecordDao.class)
	@Override
	public void setDao(UserLoginRecordDao dao) {
		super.dao = dao;
	}

	@Override
	public UserLoginRecord addUserLoginRecord(HttpServletRequest request, UserLoginInfo userLoginInfo,
			UserLoginRecordStatus loginStatus) {
		if (CommonUtil.isEmpty(userLoginInfo) || CommonUtil.isEmpty(userLoginInfo.getId())) {
			throw new BizException(10224);
		}
		UserLoginRecord userLoginRecord = new UserLoginRecord();
		userLoginRecord.setIp(RequestUtil.getIpAddr(request));
		userLoginRecord.setRequestedSessionId(request.getSession().getId());
		userLoginRecord.setLoginStatus(loginStatus);
		userLoginRecord.setClient(RequestUtil.getClientType(request));
		userLoginRecord.setClientInfo(request.getHeader("user-agent"));
		userLoginRecord.setUserInfo(userLoginInfo.getUserInfo());
		StringBuffer logDesc = new StringBuffer();
		switch (loginStatus) {
		case forcedLoginOut:
			logDesc.append("用户被强制下线，操作员：");
			break;
		case loginOvertime:
			logDesc.append("用户登陆超时，自动退出系统：");
			break;
		case userLogin:
			logDesc.append("用户登陆系统：");
			break;
		case userLoginOut:
			logDesc.append("用户登出系统：");
			break;
		case userMultipleLogin:
			logDesc.append("用户被踢下线：");
			break;
		}
		logDesc.append(userLoginInfo.getUsername());
		systemLogService.save(logDesc.toString(), SystemLogHandleType.common, getClass(), userLoginRecord.getIp(),
				userLoginInfo);
		return dao.save(userLoginRecord);
	}

	@Override
	public UserLoginRecord addUserLoginRecord(HttpSession session, UserLoginInfo userLoginInfo,
			UserLoginRecordStatus loginStatus) {
		if (CommonUtil.isEmpty(userLoginInfo) || CommonUtil.isEmpty(userLoginInfo.getId())) {
			throw new BizException(10224);
		}
		UserLoginRecord userLoginRecord = new UserLoginRecord();
		userLoginRecord.setRequestedSessionId(session.getId());
		userLoginRecord.setLoginStatus(loginStatus);
		userLoginRecord.setUserInfo(userLoginInfo.getUserInfo());
		return dao.save(userLoginRecord);
	}

}
